package com.irun.pm.utils;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
/**
 * 拍照裁剪处理类
 * @author Evy
 *
 */
public class TakePhotoUtils {
	Context context;
	public TakePhotoUtils(Context context){
		this.context = context;
	}
	/**
	 * 获取截图的Intent
	 * @param f 图片路径
	 * @param tempFile 缓存路径
	 * @param outX 切割宽度
	 * @param outY 切割高度
	 * @return
	 */
	public Intent doCropPhoto(File f,File tempFile,int outX,int outY) {
		try {
			Intent intent = new Intent("com.android.camera.action.CROP");
			intent.setDataAndType(Uri.fromFile(f), "image/*");
			intent.putExtra("crop", "true");
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("outputX", outX);
			intent.putExtra("outputY", outY);// 设置为96*96
//			if (!tempFile.exists()) {
				intent.putExtra("return-data", true);
//			}
//			intent.putExtra("output", Uri.fromFile(tempFile));
			intent.putExtra("outputFormat", "JPEG");
			return intent;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 选择本地图片并裁剪
	 * @param tempFile 路径
	 * @param outX  
	 * @param outY
	 * @return
	 */
	public Intent doPickPhotoFromGallery(File tempFile,int outX,int outY) {
		try {

			Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
			intent.setType("image/*");
			intent.putExtra("crop", "true");
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("outputX", outX);
			intent.putExtra("outputY", outY);// 设置为96*96
			intent.putExtra("outputFormat", "JPEG");
//			intent.putExtra("output", Uri.fromFile(tempFile));
//			if (!tempFile.exists()) {
				intent.putExtra("return-data", true);
//			}
			return intent;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 拍照
	 * @param PHOTO_DIR 存储路径
	 * @param mCurrentPhotoFile  图片路径
	 * @return
	 */
	public Intent doTakePhoto(File mCurrentPhotoFile) {
		try {
			// Launch camera to take photo for selected contact
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile));
			return intent;
		} catch (Exception e) {
		}
		return null;
	}
	
	public static String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"'IMG'_yyyyMMdd_HHmmss");
		return dateFormat.format(date) + ".jpg";
	}
}
